package com.zjdiepu.www.biz.base;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.CodeType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.mapper.base.DpBaseDealerInfoVoMapper;
import com.zjdiepu.www.dao.base.DpBaseDealerInfoMapper;
import com.zjdiepu.www.dao.base.DpBaseDealerInfoSubMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.dao.zx.ZxQueryCreditApplyMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoService;
import com.zjdiepu.www.ibiz.base.ISerialNumberService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfo;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfoSub;
import com.zjdiepu.www.model.base.ext.DpBaseDealerInfoExt;
import com.zjdiepu.www.model.base.vo.DpBaseDealerInfoVo;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.zx.dto.ZxQueryCreditApply;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 经销商表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月06日 16时04分<br/>
 * @since 1.0<br/>
 */
@Service
public class DpBaseDealerInfoServiceImpl extends BaseService implements IDpBaseDealerInfoService {
	public static final Logger logger = LoggerFactory.getLogger(DpBaseDealerInfoServiceImpl.class);

	@Autowired
	private DpBaseDealerInfoMapper dpBaseDealerInfoMapper;
	@Autowired
	private DpBaseDealerInfoVoMapper dpBaseDealerInfoVoMapper;
	@Autowired
	private ISerialNumberService serialNumberService;
	@Autowired
	private DpBaseDealerInfoSubMapper dpBaseDealerInfoSubMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private ZxQueryCreditApplyMapper zxQueryCreditApplyMapper;
	
	@Override
	public DpBaseDealerInfo saveChangeDealerInfo(String busiId, String dealerId) {
		YwBusiness ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
		if(null == ywBusiness) {
			ExceptionHelper.thow("999", "业务记录不存在或已经被删除");
		}
		String queryCreditApplyId = ywBusiness.getQueryCreditApplyId();
		if(StringUtils.isBlank(queryCreditApplyId)) {
			ExceptionHelper.thow("999", "征信申请ID为空");
		}
		ZxQueryCreditApply zxQueryCreditApply = zxQueryCreditApplyMapper.selectByPrimaryKey(queryCreditApplyId);
		if(null == zxQueryCreditApply) {
			ExceptionHelper.thow("999", "征信申请记录不存在或已被删除");
		}
		DpBaseDealerInfo dpBaseDealerInfo = dpBaseDealerInfoMapper.selectByPrimaryKey(dealerId);
		if(null == dpBaseDealerInfo) {
			ExceptionHelper.thow("999", "经销商记录不存在或已被删除");
		}
		zxQueryCreditApply.setDealerId(dealerId);
		zxQueryCreditApplyMapper.updateByPrimaryKeySelective(zxQueryCreditApply);
		ywBusiness.setDealerId(dealerId);
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		dpBaseDealerInfoVoMapper.mapper(dpBaseDealerInfo);
		return dpBaseDealerInfo;
		
	}
	
	@Override
	public void saveUpdateAndDelete(DpBaseDealerInfo dpBaseDealerInfo, OperateType operType) {
		switch (operType) {
		case add:
			int sNumber=serialNumberService.createSerialNumber(CodeType.DEALERINFO, null, false);
			String delearCode=  "J" + new DecimalFormat("10000").format(sNumber);
			dpBaseDealerInfo.setDealerCode(delearCode);
			dpBaseDealerInfo.initInsertData(dpBaseDealerInfo.getModifier(), dpBaseDealerInfo.getModifierName());
			dpBaseDealerInfoMapper.insert(dpBaseDealerInfo);
			
			//保存经销商子表
			DpBaseDealerInfoSub dpBaseDealerInfoSub= new DpBaseDealerInfoSub();
			dpBaseDealerInfoSub.setDealerId(dpBaseDealerInfo.getId());
			dpBaseDealerInfoSub.setAddr(dpBaseDealerInfo.getAddr());
			//增加联系人和联系人手机 modify by zzj-------------start
			dpBaseDealerInfoSub.setContactPerson(dpBaseDealerInfo.getContactPerson());
			dpBaseDealerInfoSub.setTel(dpBaseDealerInfo.getTel());
			//----------finish
			dpBaseDealerInfoSub.initInsertData(dpBaseDealerInfo.getModifier(), dpBaseDealerInfo.getModifierName());
			dpBaseDealerInfoSubMapper.insert(dpBaseDealerInfoSub);
			break;
		case edit:
			dpBaseDealerInfo.setDealerCode(null);
			dpBaseDealerInfo.initUpdateData(dpBaseDealerInfo.getModifier(), dpBaseDealerInfo.getModifierName());
			// 影响的记录数
			int updateCount = dpBaseDealerInfoMapper.updateByPrimaryKeySelective(dpBaseDealerInfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dpBaseDealerInfo.initDeleteData(dpBaseDealerInfo.getModifier(), dpBaseDealerInfo.getModifierName());
			// 影响的记录数
			int deleteCount = dpBaseDealerInfoMapper.updateByPrimaryKeySelective(dpBaseDealerInfo);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DpBaseDealerInfo> selectByExt(DpBaseDealerInfoExt dpBaseDealerInfoExt) {
		return dpBaseDealerInfoMapper.selectByExt(dpBaseDealerInfoExt);
	}
	
	@Override
	public int countByExt(DpBaseDealerInfoExt dpBaseDealerInfoExt){
		return dpBaseDealerInfoMapper.countByExt(dpBaseDealerInfoExt);
	}
	
	@Override
	public DpBaseDealerInfo selectById(String id) {
		return dpBaseDealerInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dpBaseDealerInfoMapper.clearEmpty(fieldNames, id);
		}
	}

	@Override
	public DpBaseDealerInfoVo getMulitListById(String id) {
		return dpBaseDealerInfoMapper.getMulitListById(id);
	}
}
